package com.cnpc.rightslogan.service.impl;

import com.cnpc.rightslogan.entity.Card;
import com.cnpc.rightslogan.entity.Player;
import com.cnpc.rightslogan.mapper.CardMapper;
import com.cnpc.rightslogan.mapper.PlayerMapper;
import com.cnpc.rightslogan.service.IPlayerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Create by shaojie on 2019/9/2
 */
@Service
@Transactional
public class PlayerServiceImpl implements IPlayerService {

    @Resource
    private PlayerMapper playerMapper;

    @Resource
    private CardMapper CardMapper;

    @Override
    public Player getPlayer(Integer playerId) {
        return playerMapper.selectByPrimaryKey(playerId);
    }

    /**
     * 玩家被攻击
     *
     * @param player    被攻击玩家
     * @param cardTypes 被攻击的卡牌类型
     * @return
     */
    @Override
    public Player underAttack(Player player, String cardTypes) {
        String[] cardTypeArray = cardTypes.split(",");
        for (String s : cardTypeArray) {
            Card card = new Card();
            card.setCardType(Integer.parseInt(s));
            card = CardMapper.selectOne(card);
            player.setPlayerHp(player.getPlayerHp() - card.getPhysicalDamage());
        }
        return player;
    }
}
